package com.sgx.learning.luban.homework.basictype;

import lombok.Data;
import lombok.Getter;

import java.io.IOException;
import java.io.InputStream;

@Data
public class U2 {

    @Getter
    private short value;

    public U2(short v) {
        this.value = v;
    }

    public static U2 read(InputStream is) throws IOException {
        byte[] v = new byte[2];
        try {
            is.read(v);
            short value = 0;
            for (int i = 0;i < 2;i++) {
                value <<= 8;
                value |= v[i] & 0xFF;
            }
            return new U2((short)(value & 0xFFFF));
        } catch (IOException e) {
            throw e;
        }
    }

    public String getHexValue(){
        return Integer.toHexString(value);
    }
}
